<?php if (!defined('BASEPATH'))
    exit('No direct script access allowed'); ?>
<?php

class User_Controller extends CI_Controller {

    function __construct() {
        parent::__construct();        
    }

    function get_captcha(){
        $this->load->model('captcha_model');
        return $this->captcha_model->create_captcha();
    }
    
    protected function upload_file($file_element_name, $file_path) {
        $msg = "";
        $status = "";
        
        $config['upload_path'] = $file_path;
        $config['allowed_types'] = 'pdf|docx';
        $config['max_size'] = 10 * 1024;
        $config['overwrite'] = FALSE;
        $config['encrypt_name'] = FALSE;
        $config['remove_spaces'] = TRUE;

        $this->load->library('upload', $config);
        $data = array();
        $error = "";
        if (!$this->upload->do_upload($file_element_name)) {
            $status = 'error';
//            $error = $this->upload->display_errors();
            $msg = $this->upload->display_errors('', '');
            echo "<script>alert($msg)</script>";
        } else {
            $status = 'success';
            $upload_data = $this->upload->data();
            $file_name = $upload_data['file_name'];
            $data['file_name'] = $file_name;
            $data['file_path'] = $file_path;
        }
//        $data['error'] = $error;
        $data['status'] = $status;
        $data['msg'] = $msg;
//        echo "<script>alert($msg)</script>";
        return $data; //('status' => $status, 'msg' => $msg);
    }

    function check_direct_access() {
        if ($this->input->is_ajax_request()) {

            return TRUE;
            //$this->redirect($this->get_user_type());
        }
    }

    private function check_logged_in() {
        $is_logged_in = $this->session->userdata('is_logged_in');
        return $is_logged_in;
    }

    public function get_file($file_path) {

        if (file_exists($file_path)) {
            $file_info = pathinfo($file_path);
            header('Content-Description: File Transfer');
            if($file_info["extension"] == "pdf"){
                header('Content-Type: application/pdf');
            }
            header('Content-Disposition: inline; filename=' . $file_path);
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file_path));

            readfile($file_path);
            exit();
            //$this->load->view('test_view', array('file' => $file));
        } else {
            echo $file_path;
            echo "<br>file not found";
        }
    }
}

